/**
 * 
 * If you have edited the file, please update
 * all the last-modified fields manually. Thanks!
 *
 * @last-modified by: 
 * @last-modified date: 
 * @last-modified time: 
 *
 * @author Swee Zhi
 * @date-created Sep 16, 2011    @time-created 4:53:33 PM
 *
 * @project: 
 * @module: 
 * @institution: 
 *
 */
package util.localization;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import util.enumeration.AccessRight;
import util.enumhelper.LocalizedCountry;
import util.enumhelper.LocalizedCuisineType;
import util.enumhelper.LocalizedGender;
import util.enumhelper.LocalizedLanguage;
import util.enumeration.Country;
import util.enumeration.CuisineType;
import util.enumeration.Department;
import util.enumeration.FnbType;
import util.enumeration.Gender;
import util.enumeration.JobPosition;
import util.enumeration.Language;
import util.enumhelper.LocalizedAccessRight;
import util.enumhelper.LocalizedDepartment;
import util.enumhelper.LocalizedFnbType;
import util.enumhelper.LocalizedJobPosition;

@ManagedBean
@RequestScoped
public class WebLocalization implements Serializable {
    
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private String selectedLocale;
    
    // returns the preferredLocale
    public String getPreferredLocaleFromSession() {
        String preferredLocale = "en_US";   // default to english
        
        if (ctx.getExternalContext().getSessionMap().containsKey("savedLocale")) {
            preferredLocale = ctx.getExternalContext().getSessionMap().get("savedLocale").toString();
//            System.out.println("WebLoc saved Locale " + preferredLocale);
            this.switchLocaleRootView(preferredLocale);
        }
        else {
            if (ctx.getExternalContext().getSessionMap().containsKey("selectedLocale")) {
                // use selectedLocale if no savedLocale
                preferredLocale = ctx.getExternalContext().getSessionMap().get("selectedLocale").toString();
    //            System.out.println("WebLoc chosen Locale " + preferredLocale);
                this.switchLocaleRootView(preferredLocale);
            }
        }
        
        return preferredLocale;
    }
    
    /**
     * for use with all pages' language selection dropdown except index
     */
    public void selectedLocaleValueChangeListener()
    {
        if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
        else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
        else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
        else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
        
        ctx.getExternalContext().getSessionMap().put("selectedLocale", ctx.getViewRoot().getLocale());
    }
    
    // other samples from lecturer
//    public String getLocalizedCurrencyFormat()
//    {
//        return NumberFormat.getCurrencyInstance(ctx.getViewRoot().getLocale()).format(1000);
//    }
//    
//    public String getLocalizedDateFormat()
//    {
//        return DateFormat.getDateInstance(DateFormat.LONG, ctx.getViewRoot().getLocale()).format(new Date());
//    }
    
        
    /**
     * 
     * @param accessRight (accessRight you would like it to be localized)
     * @return String
     */
    public String getLocalizedAccessRight(AccessRight accessRight)
    {
        return LocalizedAccessRight.getLocalizedAccessRightAsString(accessRight, ctx.getViewRoot().getLocale().toString());
    }
    
    
        
    /**
     * 
     * @param cuisineType (cuisine type you would like it to be localized)
     * @return String
     */
    public String getLocalizedCuisineType(CuisineType cuisineType)
    {
        return LocalizedCuisineType.getLocalizedCuisineTypeAsString(cuisineType, ctx.getViewRoot().getLocale().toString());
    }
    
    
    /**
     * 
     * @param inputValue (any NUMBER VALUE you would like it to be localized)
     * @return String
     */
    public String getLocalizedCurrencyFormat(Object inputValue)
    {
        return NumberFormat.getCurrencyInstance(ctx.getViewRoot().getLocale()).format(inputValue);
    }
    
    /**
     * 
     * @param inputDate (any date you would like it to be localized)
     * @return String
     */
    public String getLocalizedDateFormat(Date inputDate)
    {
        return DateFormat.getDateInstance(DateFormat.LONG, ctx.getViewRoot().getLocale()).format(inputDate);
    }
    
    /**
     * 
     * @param country (Country you would like it to be localized)
     * @return String
     */
    public String getLocalizedCountry(Country country)
    {
        return LocalizedCountry.getLocalizedCountryAsString(country, ctx.getViewRoot().getLocale().toString());
    }
    
    /**
     * 
     * @param fnbType (Fnb type you would like it to be localized)
     * @return String
     */
    public String getLocalizedFnbType(FnbType fnbType)
    {
        return LocalizedFnbType.getLocalizedFnbTypeAsString(fnbType, ctx.getViewRoot().getLocale().toString());
    }

    /**
     * 
     * @param gender (gender you would like it to be localized)
     * @return String
     */
    public String getLocalizedGender(Gender gender)
    {
        return LocalizedGender.getLocalizedGenderAsString(gender, ctx.getViewRoot().getLocale().toString());
    }
    
    /**
     * 
     * @param jobPosition (jobPosition you would like it to be localized)
     * @return String
     */
    public String getLocalizedJobPosition(JobPosition jobPosition)
    {
        return LocalizedJobPosition.getLocalizedJobPositionAsString(jobPosition, ctx.getViewRoot().getLocale().toString());
    }
    
    /**
     * 
     * @param department (department you would like it to be localized)
     * @return String
     */
    public String getLocalizedDepartment(Department department)
    {
        return LocalizedDepartment.getLocalizedDepartmentAsString(department, ctx.getViewRoot().getLocale().toString());
    }
    
    /**
     * 
     * @param language (language you would like it to be localized)
     * @return String
     */
    public String getLocalizedLanguage(Language language)
    {
        return LocalizedLanguage.getLocalizedLanguageAsString(language, ctx.getViewRoot().getLocale().toString());
    }
    
    // retrieve to external the above then pass in to this again...
    private void switchLocaleRootView(String selectedLocale) {
        if(selectedLocale.equals("en_US")) ctx.getViewRoot().setLocale(new Locale("en", "US"));
        else if(selectedLocale.equals("en_SG")) ctx.getViewRoot().setLocale(new Locale("en", "SG"));
        else if(selectedLocale.equals("zh_CN")) ctx.getViewRoot().setLocale(new Locale("zh", "CN"));
        else if(selectedLocale.equals("ms_MY")) ctx.getViewRoot().setLocale(new Locale("ms", "MY"));
    }
    
    
    /**
     * only for savedLocale retrieved from user account
     */
    public void putSavedLocaleToSession(String savedLocale) {
        ctx.getExternalContext().getSessionMap().put("savedLocale", savedLocale);
    }
    
}
